package ddd.ajava.chapter4.question2;
import java.util.Scanner;

public class ValueInput {

	/**
	 * 读入一个String
	 * @return
	 */
	public String input() {
		String result = null;
		try {
			Scanner scanner = new Scanner(System.in);
			result = scanner.nextLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public Object inputValue(String label, String fieldType) {
		System.out.println("请输入" + label + " : ");
		if (fieldType.equals("String")) {
			String result = input();
			return result;
		}else if (fieldType.equals("Integer")) {
			return readInteger();
		}else if (fieldType.equals("Boolean")) {
			return readBoolean();
		}
		return null;
	}
	/**
	 * 读入一个Integer类型的数
	 * @return
	 */
	private Integer readInteger(){
		String read = this.input();
		if(read != null && read.trim().equals("")){
			return null;
		}
		Integer result = null;
		try {
			result = Integer.parseInt(read);
			return result;
		} catch (Exception e) {
			System.out.println("==输入类型错误，请重新输入 ：");
			return readInteger();
		}
	}

	/**
	 * 读入一Boolean值
	 * @return
	 */
	private Boolean readBoolean(){
		String temp = input();
		if(temp != null && temp.trim().equals("")){
			return null;
		}
		while (!(temp.equals("true") || temp.equals("false"))) {
			System.out.println("输入类型错误，请重新输入 ：");
			temp = input();
		}
		if(temp.equals("true")){
			return true;
		}
		return false;
	}
	
}
